package com.ljb.hiknvrdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.ljb.lib_hiknvr.nvr.ThreadUtils;
import com.ljb.lib_hiknvr.nvr.controller.Callback;
import com.ljb.lib_hiknvr.nvr.controller.HikNVRController;
import com.ljb.lib_hiknvr.nvr.controller.PTZCommand;
import com.ljb.lib_hiknvr.nvr.play.NVRPlayView;
import com.ljb.lib_hiknvr.nvr.play.OnSimplePlayStateListener;

/**
 * Created by ljb on 2018/9/29.
 */
public class NVRFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "NVRFragment";
    private NVRPlayView pv;
    private HikNVRController controller;
    private int[] data;
    private String ipAddr = "";
    private int port = 80;
    private String user = "";
    private String pwd = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_nvr, container, false);
        initUI(rootView);
        return rootView;
    }

    private void initUI(View rootView) {
        rootView.findViewById(R.id.bt_login).setOnClickListener(this);
        rootView.findViewById(R.id.bt_logout).setOnClickListener(this);
        rootView.findViewById(R.id.bt_start).setOnClickListener(this);
        rootView.findViewById(R.id.bt_stop).setOnClickListener(this);
        rootView.findViewById(R.id.bt_up).setOnClickListener(this);
        rootView.findViewById(R.id.bt_down).setOnClickListener(this);
        rootView.findViewById(R.id.bt_left).setOnClickListener(this);
        rootView.findViewById(R.id.bt_right).setOnClickListener(this);
        rootView.findViewById(R.id.bt_capture).setOnClickListener(this);
        rootView.findViewById(R.id.bt_record).setOnClickListener(this);


        pv = rootView.findViewById(R.id.pv);
        pv.setOnTapClickListener(new NVRPlayView.OnTapClickListener() {
            @Override
            public void onSingleTap() {
                L.e(TAG, "onSingleTap: ");
            }

            @Override
            public void onDoubleTap() {
                L.e(TAG, "onDoubleTap: ");
            }
        });

        controller = HikNVRController.getInstance();
        controller.initSDK();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //注册
            case R.id.bt_login:
                ThreadUtils.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        data = controller.login(ipAddr, port, user, pwd);
                        controller.getChanState(data[0]);
                    }
                });


                break;
            //注销
            case R.id.bt_logout:
                ThreadUtils.runInThread(new Runnable() {
                    @Override
                    public void run() {
                        controller.logout(data[0]);
                    }
                });
                break;
            //开始预览
            case R.id.bt_start:
                pv.startPlay(data[0], data[1] + 1, 1, new OnSimplePlayStateListener() {
                    @Override
                    public void onStartError() {
                        L.e(TAG, "onStartError: ");
                    }

                    @Override
                    public void onStartSuccess() {
                        L.e(TAG, "onStartSuccess: ");
                    }
                });
                break;
            //停止预览
            case R.id.bt_stop:
                pv.stopPlay(new OnSimplePlayStateListener() {
                    @Override
                    public void onStopError() {
                        L.e(TAG, "onStopError: ");
                    }

                    @Override
                    public void onStopSuccess() {
                        L.e(TAG, "onStopSuccess: ");
                    }
                });
                break;
            //云台控制
            case R.id.bt_up:
                pv.ptzControl(PTZCommand.TILT_UP, new Callback<Boolean>() {
                    @Override
                    public void callback(Boolean ret) {
                        L.e(TAG, "callback: " + ret);
                    }
                });
                break;

            case R.id.bt_down:
                pv.ptzControl(PTZCommand.TILT_DOWN, new Callback<Boolean>() {
                    @Override
                    public void callback(Boolean ret) {
                        L.e(TAG, "callback: " + ret);
                    }
                });
                break;
            case R.id.bt_left:
                pv.ptzControl(PTZCommand.PAN_LEFT, new Callback<Boolean>() {
                    @Override
                    public void callback(Boolean ret) {
                        L.e(TAG, "callback: " + ret);
                    }
                });
                break;
            case R.id.bt_right:
                pv.ptzControl(PTZCommand.PAN_RIGHT, new Callback<Boolean>() {
                    @Override
                    public void callback(Boolean ret) {
                        L.e(TAG, "callback: " + ret);
                    }
                });
                break;
            //截图
            case R.id.bt_capture:
                pv.capture(null, new Callback<Boolean>() {
                    @Override
                    public void callback(Boolean ret) {
                        L.e(TAG, "callback: " + ret);
                    }
                });
                break;
            //录像
            case R.id.bt_record:
                Button bt = (Button) view;
                if (TextUtils.equals(bt.getText(), "开始录像")) {
                    pv.startRecord(null, null);
                    bt.setText("停止录像");
                } else {
                    pv.stopRecord(null);
                    bt.setText("开始录像");
                }
                break;
        }
    }

    static class L {
        public static void e(String tag, String msg) {
            Log.e(tag, msg);
        }
    }
}
